package com.djahva.image;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

/**
 * Created by IntelliJ IDEA.
 * User: geert
 * Date: Jun 13, 2008
 * Time: 9:00:51 PM
 * To change this template use File | Settings | File Templates.
 */
public class FileImageProvider implements ImageProvider{

    public BufferedImage getImage(ImageRequest imageRequest, String location) throws IOException {
        BufferedImage buff = null;
        File file = new File(location,imageRequest.getImg());
        // todo optimize to directly load to resized version instead of full picture if possible
        imageRequest.log("Read Image from file "+file);
        try{
        	buff = ImageIO.read(file);
        	imageRequest.log("Image found");
        	return buff;
        } catch(IOException e){
        	imageRequest.log("Image not found, switching to default");
        	String notfound = (String)imageRequest.properties.get("notfound");
        	file = new File(notfound);
        	imageRequest.log("Read Image from file "+file);
        	return ImageIO.read(file);
        }
    }
}

